package com.fone.player.view;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.PopupWindow.OnDismissListener;

import com.fone.player.R;
import com.fone.player.util.ScreenUtil;

public class LocalVedioOperationPopWindow implements OnClickListener {
	private PopupWindow mPopupWindow;
	private View mView;
	private Activity mActivity;
	private int popWidth = 150;
	private RelativeLayout rl_delete;
	private RelativeLayout rl_entry;
	private TextView tv_entry;
	private IOperationListener listener;
	private ImageView iv_encry;
	private boolean isEncryption;// 是否加密

	/**
	 * 
	 * @param activity
	 * @param isEncryption
	 *            加密或者解密
	 */
	public LocalVedioOperationPopWindow(Activity activity, boolean isEncryption) {
		this.mActivity = activity;
		this.isEncryption = isEncryption;
		mView = LayoutInflater.from(mActivity).inflate(
				R.layout.local_media_operation_pop_view, null);
		initView();
	}

	public void setOperationListener(IOperationListener listener) {
		this.listener = listener;
	}

	@SuppressWarnings("deprecation")
	private void initView() {
		mPopupWindow = new PopupWindow(mView, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		popWidth = ScreenUtil.dp2px(80);
		mPopupWindow.setWidth(popWidth);
		mPopupWindow.setBackgroundDrawable(new BitmapDrawable());// 为了点击其他地方
																	// popupWindow能够消失
		mPopupWindow.setFocusable(true);
		mPopupWindow.setOutsideTouchable(true);
		mPopupWindow.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss() {
				if (listener != null) {
					listener.dismiss();
				}
			}
		});
		rl_delete = (RelativeLayout) mView.findViewById(R.id.rl_delete);
		tv_entry = (TextView) mView.findViewById(R.id.tv_enctry);
		rl_entry = (RelativeLayout) mView.findViewById(R.id.rl_enctry);
		iv_encry = (ImageView) mView.findViewById(R.id.iv_enctry);
		if (isEncryption) {
			tv_entry.setText("加密");
			iv_encry.setBackgroundResource(R.drawable.local_icon_encryption_selector);
		} else {
			tv_entry.setText("解密");
			iv_encry.setBackgroundResource(R.drawable.local_icon_decode_selector);
		}
		rl_delete.setOnClickListener(this);
		rl_entry.setOnClickListener(this);
	}

	public void showPopupWindow(View view) {
		if (mPopupWindow != null) {
			int[] location = new int[2];
			view.getLocationOnScreen(location);
			int x = location[0] + view.getWidth() - popWidth;
			int y = location[1] + view.getHeight();
			mPopupWindow.showAtLocation(view, Gravity.LEFT | Gravity.TOP, x, y);
		}
	}

	public void dismiss() {
		if (mPopupWindow != null) {
			mPopupWindow.dismiss();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rl_delete:
			if (listener != null) {
				listener.delete();
			}
			dismiss();
			break;
		case R.id.rl_enctry:
			if (listener != null) {
				listener.encryOrdecode();
			}
			dismiss();
			break;

		default:
			break;
		}
	}

	public interface IOperationListener {
		// 加密或者解密
		void encryOrdecode();

		void delete();

		void dismiss();
	}
}
